// Copyright 2023 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef VMI_UTIL_STREAM_H
#define VMI_UTIL_STREAM_H

#include <string>
#include <sys/time.h>
#include <unistd.h>
#include "json/cJSON.h"

#define VMI_REINTERPRET_CAST_POINTER(type, ptr) reinterpret_cast<type *>(ptr)

/**
* @brief : determine whether this package is in the white list
* @param [in]packageName: the packageName name
* @return : false: the package is not in the white list
            true: the package is in the white list
*/
bool IsPkgInWhitelist(const std::string packageName);

/**
* @brief : determine whether the process is alive
* @param [in]procName: process name
* @return : false: process is died or process name is error
            true: process is still alive
*/
bool IsProcessAlive(const std::string procName);

/**
* @brief : kill the process by package name using killall cmd
* @param [in]package: package name
*/
void KillAllProcess(const std::string package);

/**
* @brief : kill the process by package name
* @param [in]package: package name
*/
void KillProcess(const std::string package);

/**
* @brief : start the package's process and the activity
* @param [in]package: package name
* @param [in]activity: activity name
*/
void StartProcess(const std::string package, const std::string activity);

/**
* @brief : executive shell cmd and save the result
* @param [in]cmdLine: shell command
* @param [out]cmdOutput: result fo execution
* @return : -1: the length of result is too short or command is error
            pos: the length of result
*/
int ShellCmd(const std::string cmdLine, std::string& cmdOutput);

/**
* @brief : return home interface
*/
void Restore2Home(void);

/**
* @brief : clear all the chain
*/
void IptablesF(void);

/**
* @brief : write the char to a file
* @param [in]dataPath: first object:the path of file
* @param [in]data: first object:char to be writen
                   second object:the length of char
* @return : char to be writen
*/
const uint8_t* SaveFile(const std::string dataPath, std::pair<const uint8_t*, uint32_t> data);

/**
* @brief : determine if the key-value is right
* @param [in]cJson: first object:cJSON object
                    second object:input key
* @param [out]value: value
* @return : false: the key-value is wrong
            true: the key-value is right
*/
bool GetInt32(std::pair<cJSON *, const uint8_t *> cJson, int& value);

/**
* @brief : determine if the key-value is right and set the default value if it return false
* @param [in]cJson: first object:cJSON object
                    second object:input key
* @param [out]value: value
* @param [in]defaultValue: default value
*/
void GetInt32Default(std::pair<cJSON *, const uint8_t *> cJson, int& value, int defaultValue);

/**
* @brief : Calculate call function frequency
*/
void FunctionCallFreq(void);
#endif
